package day01;


import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author feng
 * @Date 2021/12/24 15:33
 * 创建文件的三种方式
 */

public class FileCreate {

    @Test
    public void test() {
        //1.开辟内存，
        File file = new File("D:\\IO\\1.text");
        try {
            //2.创建文件
            boolean newFile = file.createNewFile();
            System.out.println("常见文件成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据父目录文件路径+子路径构建
     */
    @Test
    public void test02() {
        //父路径对象
        File parentFile = new File("D:\\IO");
        //子文件
        String fileName = "2.text";
        File file = new File(parentFile, fileName);
        try {
            boolean newFile = file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void Test03() throws IOException {
        //路径
        String parentPath = "D:\\IO";
        String childrenPath = "4.text";
        File file = new File(parentPath, childrenPath);
        file.createNewFile();
        System.out.println("文件创建成功");
    }
}
